package com.ynunicom.midplatform.common.result;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import javax.servlet.http.HttpServletResponse;

/**
 * Json通用返回类
 *
 * @author duwei
 * @version 2018-05-04 16:30
 */
@Getter
@Setter
@Builder
public class ResponseEntity<T> {

    final static int FAILED_CODE = 0;
    final static int SUCCESS_CODE = 1;

    final static String DEFAULT_FAILED_MSG = "失败";
    final static String DEFAULT_SUCCESS_MSG = "成功";
    @JSONField(ordinal = 1)
    private int status;
    @JSONField(ordinal = 2)
    private int code;
    @JSONField(ordinal = 3)
    private String msg;
    @JSONField(ordinal = 4)
    private T data;

    public static ResponseEntity success() {
        return ResponseEntity.builder()
                .code(HttpServletResponse.SC_OK)
                .status(SUCCESS_CODE)
                .msg(DEFAULT_SUCCESS_MSG)
                .build();
    }

    public static ResponseEntity success(String msg) {
        return ResponseEntity.builder()
                .code(HttpServletResponse.SC_OK)
                .status(SUCCESS_CODE)
                .msg(msg)
                .build();
    }

    public static ResponseEntity success(int code, String msg) {
        return ResponseEntity.builder()
                .status(SUCCESS_CODE)
                .code(code)
                .msg(msg)
                .build();
    }


    public static <T> ResponseEntity<T> success(int code, String msg, T data) {
        return ResponseEntity.<T>builder()
                .status(SUCCESS_CODE)
                .code(code)
                .msg(msg)
                .data(data)
                .build();
    }

    public static <T> ResponseEntity<T> success(String msg, T data) {
        return ResponseEntity.<T>builder()
                .code(HttpServletResponse.SC_OK)
                .status(SUCCESS_CODE)
                .msg(msg)
                .data(data)
                .build();
    }

    public static <T> ResponseEntity<T> success(T data) {
        return ResponseEntity.success(DEFAULT_SUCCESS_MSG, data);
    }

    public static ResponseEntity failed() {
        return ResponseEntity.builder()
                .status(FAILED_CODE)
                .msg(DEFAULT_FAILED_MSG)
                .build();
    }

    public static ResponseEntity failed(String msg) {
        return ResponseEntity.builder()
                .status(FAILED_CODE)
                .msg(msg)
                .build();
    }

    public static ResponseEntity failed(int code, String msg) {
        return ResponseEntity.<String>builder()
                .status(FAILED_CODE)
                .code(code)
                .msg(msg)
                .build();
    }


    public static <T> ResponseEntity<T> failed(int code, String msg, T data) {
        return ResponseEntity.<T>builder()
                .status(FAILED_CODE)
                .code(code)
                .msg(msg)
                .data(data)
                .build();
    }

    public static <T> ResponseEntity<T> failed(String msg, T data) {
        return ResponseEntity.<T>builder()
                .status(FAILED_CODE)
                .msg(msg)
                .data(data)
                .build();
    }

}
